<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title> 添加排课记录 </title>
</head>
<body>
<?php
require("view_header.php");
//1. 登录用户都可以查看
$isAllowAccess = isset($_SESSION['administrator']);
$isAllowAccess |= isset($_SESSION['jgadmin']);
if (!$isAllowAccess ){
    echo "<a href='../loginpage.php'>Please Login First!</a>";
    exit(1);
}
?>
<div class="col-lg-offset-1 col-lg-10 col-sm-12 col-xs-12">
    <div id="course_schedule_info" class="panel panel-default" style="margin-left: -1.3rem; margin-right: -1.3rem;">
        <div class="panel-body">
            <form class = "form-horizontal" @submit.prevent="postRecord()" method="post">
                <div class="form-group">
                    <label class="col-sm-1 control-label">标题:</label>
                    <div class="col-sm-4">
                        <input v-model="recordDetail.title" class="form-control" placeholder="标题" type="text">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12 col-xs-12 col-lg-12">
                            <textarea v-model="recordDetail.content"
                                      class="form-control"
                                      style="min-height: 60rem;"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-offset-4 col-sm-1 col-lg-1 col-xs-4">
                        <button type="submit" class="form-control btn btn-primary" >提交</button>
                    </div>
                    <div class="col-sm-1 col-lg-1 col-xs-4">
                        <button @click="returnBack()" type="button" class="form-control btn btn-primary" >返回</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var app = new Vue({
        el: "#course_schedule_info",
        data:{
            recordDetail: {
                id:"",
                title:"",
                content:"",
            }
        },

        created:function () {
            this.loadRecordInfo();
        },

        methods:{

            // 加载cookie中的用户信息
            loadRecordInfo:function() {
                this.recordDetail.id = getUrlKey('id');
                if ( ! isNull(this.recordDetail.id) ) {
                    this.getRecordDetail(this.recordDetail.id);
                }
            },

            getRecordDetail:function (id) {
                let reqData = {};
                reqData.id = id;
                reqData.op = "fetch";
                let param = "para=" + JSON.stringify(reqData);
                new Promise(function (resolve, reject) {
                    $.ajax({
                        type: 'post',
                        url: '../model/course_schedule_router.php',
                        data: param,
                        dataType: 'json',
                        success: function (retData) {
                            if (retData.status) {
                                resolve(retData.dat);
                            }
                            else {
                                reject(retData.msg);
                            }
                        },
                        error:function (dat) {
                            //alert("dddeeee");
                        },
                    });
                }).then(function (detail) {
                    app.recordDetail.id = detail.id;
                    app.recordDetail.title = detail.title;
                    app.recordDetail.content = Base64.decode(detail.content);
                }).catch(function (errMsg) {
                    alert('获取代码信息失败:'+errMsg);
                });
            },

            postRecord:function() {
                if ( this.recordDetail.title == "" ) {
                    alert('标题不能为空');
                    return;
                }

                if (  this.recordDetail.content < 5 ) {
                    alert('正文内容太少');
                    return;
                }
                if ( this.recordDetail.id != null ) {
                    this.editRecord();
                }
                else {
                    this.addRecord();
                }
            },

            editRecord:function () {
                let reqData = {};
                reqData.id = this.recordDetail.id;
                reqData.title = encodeURIComponent(this.recordDetail.title);

                // 去除末尾多余的换行符
                this.recordDetail.content = this.recordDetail.content.replace(/^\s+|\s+$/g,'');
                // base64加密
                let base64Str = Base64.encode(this.recordDetail.content);
                // url编码，避免 +、=在传输过程中的干扰
                reqData.content = encodeURIComponent(base64Str);
                reqData.op = "edit";
                let param = "para="+JSON.stringify(reqData)
                new Promise(function(resolve, reject) {
                    $.ajax({
                        type:'post',
                        url:'../model/course_schedule_router.php',
                        data:param,
                        dataType:'json',
                        success:function (retData) {
                            if ( retData.status ) {
                                resolve();
                            }
                            else {
                                reject(retData.msg);
                            }
                        }
                    });
                }).then(function() {
                    history.go(-1);
                }).catch(function(errMsg) {
                    alert('修改文本失败:'+errMsg);
                });
            },

            addRecord:function () {
                let reqData = {};
                reqData.title = encodeURIComponent(this.recordDetail.title);

                // 去除末尾多余的换行符
                this.recordDetail.content = this.recordDetail.content.replace(/^\s+|\s+$/g,'');
                // base64加密
                let base64Str = Base64.encode(this.recordDetail.content);
                // url编码，避免 +、=在传输过程中的干扰
                reqData.content = encodeURIComponent(base64Str);
                reqData.op = "add";
                let param = "para="+JSON.stringify(reqData);
                new Promise(function(resolve, reject) {
                    $.ajax({
                        type:'post',
                        url:'../model/course_schedule_router.php',
                        data:param,
                        dataType:'json',
                        success:function (retData) {
                            if ( retData.status ) {
                                resolve();
                            }
                            else {
                                reject(retData.msg);
                            }
                        }
                    });
                }).then(function() {
                    history.go(-1);
                }).catch(function(errMsg) {
                    alert('添加文本失败:'+errMsg);
                });
            },

            returnBack:function () {
                history.go(-1);
            },
        }
    });
</script>
</body>
</html>
<?php
//require("../oj-footer.php");
?>